Python Operators

Operators are symbols in Python that are used to manipulate variables and values. Python has a wide range of operators that fall into various categories:

Arithmetic Operators:

Arithmetic operators are used to perform basic mathematical operations. 

Operator

Description

Example

+

Addition

a + b

-

Subtraction

a - b

*

Multiplication

a * b

/

Division

a / b

//

Floor Division (quotient)

a // b

%

Modulus (remainder)

a % b

**

Exponentiation

a ** b

 Example:

a = 10

b = 3

print(a + b)  # 13 (Addition)

print(a - b)  # 7 (Subtraction)

print(a * b)  # 30 (Multiplication)

print(a / b)  # 3.333 (Division)

print(a // b) # 3 (Floor Division)

print(a % b)  # 1 (Modulus)

print(a ** b) # 1000 (Exponentiation)


Comparison Operators:
Comparison operators are used to compare two values and return a boolean value.

Operator

Description

Example

==

Equal to

a == b

!=

Not equal to

a != b

> 

Greater than

a > b

< 

Less than

a < b

>=

Greater than or equal to

a >= b

<=

Less than or equal to

a <= b


Example:
a = 10
b = 3
print(a == b)  # False (Equal to)
print(a != b)  # True (Not equal to)
print(a > b)   # True (Greater than)
print(a < b)   # False (Less than)
print(a >= b)  # True (Greater than or equal to)
print(a <= b)  # False (Less than or equal to)

Logical Operators

Logical operators are used to combine conditional statements.

Operator

Description

Example

and

Returns True if both operands are true

a and b

or

Returns True if at least one operand is true

a or b

not

Reverses the logical state

not a


Example:
a = True
b = False
print(a and b)  # False (Both need to be true)
print(a or b)   # True (At least one is true)
print(not a)    # False (Reverses the value of a)


Assignment Operators

Assignment operators are used to assign values to variables.

 

Operator

Description

Example

=

Assigns value to variable

a = 5

+=

Adds and assigns

a += 3

-=

Subtracts and assigns

a -= 3

*=

Multiplies and assigns

a *= 3

/=

Divides and assigns

a /= 3

//=

Floor divides and assigns

a //= 3

%=

Modulus and assigns

a %= 3

**=

Exponentiates and assigns

a **= 3



Example:

a = 10

a += 5  # a = a + 5 → a = 15

a -= 3  # a = a - 3 → a = 12

a *= 2  # a = a * 2 → a = 24

a /= 4  # a = a / 4 → a = 6.0

Identity Operators

Identity operators are used to compare the memory location of two objects.

Operator

Description

Example

is

Returns True if both variables point to the same object in memory

a is b

is not

Returns True if both variables do not point to the same object in memory

a is not b


Example:

a = [1, 2, 3]

b = [1, 2, 3]

c = a

print(a is b)     # False (a and b are different objects)

print(a is c)     # True (a and c are the same object)

print(a is not b) # True (a and b are different objects)


Membership Operators
Membership operators are used to test whether a value is in a sequence (such as a list, tuple, or string).

Operator

Description

Example

in

Returns True if the value is found in the sequence

a in b

not in

Returns True if the value is not found in the sequence

a not in b


Example:
a = [1, 2, 3, 4, 5]
print(3 in a)     # True (3 is in the list)
print(6 not in a) # True (6 is not in the list)

Bitwise Operators

Bitwise operators are used to perform bit-level operations on integers.

Operator

Description

Example

&

Bitwise AND

a & b

`

`

Bitwise OR

^

Bitwise XOR

a ^ b

~

Bitwise NOT

~a

<< 

Bitwise left shift

a << 2

>> 

Bitwise right shift

a >> 2


Example:
a = 5  # (binary: 0101)
b = 3  # (binary: 0011)
print(a & b)  # 1 (binary: 0001, bitwise AND)
print(a | b)  # 7 (binary: 0111, bitwise OR)
print(a ^ b)  # 6 (binary: 0110, bitwise XOR)
print(~a)     # -6 (bitwise NOT)
print(a << 1) # 10 (binary: 1010, left shift)
print(a >> 1) # 2  (binary: 0010, right shift)

Ternary Conditional Operator (Conditional Expression)

The ternary conditional operator allows you to evaluate a condition and choose between two values based on the result.

Operator

Description

Example

x if condition else y

Returns x if the condition is True, otherwise returns y

a if a > b else b


Example:
a = 10
b = 5
result = a if a > b else b
print(result)  # 10 (because a is greater than b)










← Back Next →

Comments

Popular posts from this blog

Wrapper Class

Information Security & Essential Terminology

Information Security Threat Categories